package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.support.Result;
import com.itheima.util.DateUtil;
import org.apache.dubbo.config.annotation.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(timeout = 60000)
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public boolean importData(List<String[]> data) {
        List<OrderSetting> list = new ArrayList<>();
        for(String[] row : data){
            //这里读取了两行，第一行是日期，第二行是可预约数量
            String orderDate = row[0];
            Integer number = Integer.parseInt(row[1]);
            //判断日期对应的设置是否存在
            //去数据库查找是否有这一天
            OrderSetting orderSetting = findByDate(orderDate);
            if(orderSetting == null){
                orderSetting = new OrderSetting();
                orderSetting.setOrderDate(DateUtil.parseDate(orderDate));
            }
            orderSetting.setNumber(number);
            list.add(orderSetting);
        }
        //保存所有信息
        //当数据过多时候应该分批保存，没讲
        this.saveOrUpdateBatch(list);
        return true;
    }

    @Override
    public Result selectByYearMonth(int year, int month) {

        Map map = new HashMap();
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        //yyyy-MM
        //小于10时候加一个0
        String yearMonth = year + "-" +(month<10?"0":"") + month;
        queryWrapper.eq("date_format(order_date,'%Y-%m')",yearMonth);
        //select t.*,date_format(order_date,'%Y-%m') from t_order_setting t
        //where date_format(order_date,'%Y-%m') = '2020-11'
        List<OrderSetting> dataList = this.list(queryWrapper);
        //settingData: {//前端展示数据需要的数据格式
        //        '2020-11-01':{number:200,reservations:200},
        //        '2020-11-02':{number:200,reservations:100},
        //        '2020-11-15':{number:200,reservations:0}
        //      },
        for(OrderSetting orderSetting : dataList){
            String orderDate = DateUtil.formatDate(orderSetting.getOrderDate());
            map.put(orderDate,orderSetting);
        }
       return new Result(true,"200",map);
    }

    @Override
    public void updatemax(String date, Integer num) {
        OrderSetting orderSetting = new OrderSetting();
        orderSetting.setNumber(num);
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",date);
        this.update(orderSetting,queryWrapper);
    }


    private OrderSetting findByDate(String orderDate) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }
}
